// 课程管理store
import {
    deleteStuRecord,
    getClass,
    getClassNew,
    getGrade,
    getMajor,
    getStudentInfo,
    massDeleteStuRecord,
    resetStuPwd,
    searchStu,
    teacherImport,
    studentDownload,
    studentImport,
    studentInfo,
    studentPreview,
    studentUpload
} from "@/api/user"

const state = {
    grades:[],
    simpleStudentInfo:[],
    count:'',//查询学生总数
    //学生详细信息
    studentInfo:{
        simpleStudentInfo: {
            studentId: "",
            account: "",
            password: "",
            school: "",
            name: "",
            stuNumber: ""
        },
        alias: "",
        className: "",
        college: "",
        phone: "",
        email: ""
    },

    current: 0, // 存储批量导入的时候是第几步,
    uploadState: 0,//0上传学生，1上传老师
    userRecord: [], // 存储导入文件中的学生信息
    studentRecord: [], //学生列表记录
    teacherRecord:[],  //老师列表记录
    classList:[]
}

const mutations = { // mutations改变state中的数据
    NULL:()=>{},

    SET_UPLOAD_STATE:(state,data)=>{
        state.uploadState = data
    },

    DEL_STUDENT_UPLOAD:(state,data)=>{
        state.studentRecord = state.studentRecord.filter((i)=>{
            return i.index!==data;
        })
    },

    SET_STUDENT_UPLOAD:(state,data)=>{
        let arr = []
        let k=0
        data.forEach(i=>{
            i.index = k
            k++
            arr.push(i)
        })
        state.studentRecord = arr
    },

    DEL_TEACHER_UPLOAD:(state,data)=>{
        console.log(data)
        state.teacherRecord=state.teacherRecord.filter((i)=>{
            return i.index !== data;
        })
        console.log(state.teacherRecord)
    },

    SET_TEACHER_UPLOAD:(state,data)=>{
        let arr = []
        let k=0
        data.forEach(i=>{
            i.index = k
            k++
            arr.push(i)
        })
        state.teacherRecord = arr
    },

    SET_CURRENT_STEP:(state,data)=>{
      state.current=data
    },

    SET_GRADES:(state,data)=>{
      state.grades= data;
    },
    SET_SIMPLE_STUDENT_INFO:(state,data)=>{
        const array=[]
        for(let i of data){
            array.push({
                key:i.studentId,
                stuNumber:i.stuNumber,
                name:i.name,
                password:i.password,
                school:i.school
            })
        }
        state.simpleStudentInfo=array;
    },
    SET_COUNT:(state,count)=>{
      state.count=count
    },
    SET_STUDENT_INFO:(state,data)=>{
        state.studentInfo.simpleStudentInfo=data.simpleStudentInfo;
        state.studentInfo.alias=data.alias;
        state.studentInfo.className=data.className;
        state.studentInfo.college=data.college;
        state.studentInfo.phone=data.phone;
        state.studentInfo.email=data.email;
    },
    SET_USER_RECORD: (state, userRecord) => { // 修改导入课程中的课程数据
        state.userRecord = userRecord
    },
    SET_STU_RECORD: (state,data) => { //导入学生到state
        state.studentRecord = data
    },
    DELETE_USER: (state, id) => { // 删除courseId的课程元素
        for(var i = 0; i < state.userRecord.length; i ++){
            if(state.userRecord[i].userId === id) {
                state.userRecord.splice(i, 1)
                break;
            }
        }
    },
    RESETPWD_STU: (state,data) => { //重置学生密码为账号
        for(let i = 0; i < state.studentRecord.data.list.length; i++){
            if (state.studentRecord.data.list[i].account === data.account) {
                state.studentRecord.data.list[i].password = state.studentRecord.data.list[i].account
            }
        }
    },
    DELETE_STU: (state, studentID) => { // 删除学生列表记录
        for(let i = 0; i < state.studentRecord.data.list.length; i++){
            if(state.studentRecord.data.list[i].studentID === studentID){
                state.studentRecord.data.list.splice(i, 1)
                break;
            }
        }
    },
    MASS_DELETE_STU: (state, studentIDs) => { //批量删除学生列表数据
        for(let i = 0; i < state.studentRecord.data.list.length; i++){
            for(let j = 0; j < studentIDs.length; j++){
                if(state.studentRecord.data.list[i].studentID === studentIDs[j]){
                    state.studentRecord.data.list.splice(i, 1)
                }
            }
        }
    },
    SELECT_STU: (state,selectValue) => { //下拉列表处理
        let newstudentRecord1 = [];
        for (let i = 0; i < state.studentRecord.data.length; i++) {
            if (state.studentRecord.data[i].studentID.indexOf(selectValue.selectValue) >= 0) {
                newstudentRecord1.push(state.studentRecord.data[i])
            }
        }
        state.stuselectRecord = newstudentRecord1
    }
}

const actions = { // 发送请求
    userUpload({ commit }, file){ // 批量导入上传文件
        return new Promise((resolve, reject) => {
            studentUpload(file).then( data => {
                if(data.code === 1){ // 上传成功
                    commit('SET_CURRENT_ONE');
                    commit('SET_USER_RECORD', data.data);
                    resolve(data);
                }else{
                    reject(data)
                }
            })
        })
    },
    userDownload(){ // 下载学生模板
        return new Promise((resolve, reject) => {
            studentDownload().then((data) => {
                if(data.code === 1){
                    resolve(data);
                }else{
                    reject(data);
                }
            })
        })
    },
    userPreview(){ // 预览学生模板
        return new Promise((resolve, reject) => {
            studentPreview().then((data) => {
                if(data.code === 1){
                    resolve(data);
                }else{
                    reject(data);
                }
            })
        })
    },
    backPre({commit}){ // 回到上一步，改变current的值
        commit('SET_CURRENT_ZERO')
    },
    userImport({commit, state}, data){
        const token = data.token;
        const type = data.type;
        const collegeId = data.collegeId;
        if(type===0) {//学生导入
            return new Promise((resolve, reject) => {
                studentImport({students: state.studentRecord, token: token, collegeId:collegeId})
                    .then((data) => {
                        if (data.code === 1) {
                            commit('SET_CURRENT_ZERO')
                            resolve(data);
                        } else {
                            reject(data);
                        }
                    })
            })
        }else if(type===1) {//教师导入
            return new Promise((resolve, reject) => {
                teacherImport({teachers: state.teacherRecord, token: token, collegeId:collegeId})
                    .then((data) => {
                        if (data.code === 1) {
                            commit('SET_CURRENT_ZERO')
                            resolve(data);
                        } else {
                            reject(data);
                        }
                    })
            })
        }
    },
    userDelete({commit}, data){ // 删除用户
        const id = data.userId
        return new Promise((resolve) => {
            commit('DELETE_USER', id);
            resolve(data)
        })
    },
    getStudentlist({commit},infoData) { //获取学生列表数据
        const info = {
            currPage: infoData.currPage,
            rows: infoData.rows,
            collegeId: infoData.collegeId
        }
        return new Promise((resolve,reject) => {
            studentInfo({info: info}).then((data) => {
                if(data.code === 1){
                    commit('SET_STU_RECORD',data);
                    resolve(state.studentRecord)
                }else {
                    reject(data);
                }
            })
        })
    },

    //查看学生详细信息
    checkStuInfo({commit},data){
        return new Promise((resolve, reject) => {
            getStudentInfo(data).then(res => {
                console.log(res)
                if (res.code === 1) {
                    commit('SET_STUDENT_INFO', res.data);
                    resolve(res)
                }else{
                    reject(res.msg)
                }
            }).catch(err => {
                reject(err)
            })
        })
    },

    //重置学生密码为学生账号
    resetStuPwd({commit},data) {
        return new Promise((resolve, reject) => {
            resetStuPwd(data).then(res => {
                if (res.code === 1) {
                    commit('NULL');
                    resolve(res)
                }else{
                    reject(res.msg)
                }
            }).catch(err => {
                reject(err)
            })
        })
    },
    //删除单条学生
    deleteStu({commit},data) {
        return new Promise((resolve, reject) => {
            deleteStuRecord(data).then(res => {
                if (res.code === 1) {
                    commit("NULL")
                    resolve(res)
                }else{
                    reject(res.msg)
                }
            }).catch(err => {
                reject(err)
            })
        })
    },
    //批量删除学生
    massDeleteStuRecord({commit},data) {
        return new Promise((resolve, reject) => {
            massDeleteStuRecord(data).then(res => {
                console.log(res)
                if (res.code === 1) {
                    resolve(res)
                    commit("NULL")
                }else{
                    reject(res.msg)
                }
            }).catch(err => {
                reject(err)
            })
        })
    },
    //获取年级
    getGrades({commit},info) {
        return new Promise((resolve,reject) => {
            getGrade(info).then((data) => {
                //console.log(data)
                if(data.code === 1){
                    commit("NULL")
                    resolve(data)
                }else{
                    reject(data.msg)
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },
    //获取专业
    getMajors({commit},info) {
        return new Promise((resolve,reject) => {
            getMajor(info).then((data) => {
                if(data.code === 1){
                    commit("NULL")
                    resolve(data);
                }else{
                    reject(data.msg)
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },
    //获取班级
    getClassNumbers({commit},info) {
        return new Promise((resolve,reject) => {
            getClass(info).then((data) => {
                if(data.code === 1){
                    commit('NULL');
                    resolve(data);
                }else{
                    reject(data)
                }
            })
        })
    },
    //获取班级返回类型不同
    getClassNumbersNew({commit},info) {
        return new Promise((resolve,reject) => {
            getClassNew(info).then((data) => {
                if(data.code === 1){
                    commit('NULL');
                    resolve(data);
                }else{
                    reject(data)
                }
            })
        })
    },
    //查询学生列表
    getSimpleStudentInfo({commit},info) {
        return new Promise((resolve,reject) => {
            studentInfo(info).then((data) => {
                //console.log(data)
                if(data.code === 1){
                    commit('SET_SIMPLE_STUDENT_INFO',data.data.data)
                    commit('SET_COUNT',data.data.count)
                    resolve(data);
                }else{
                    reject(data.msg)
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },
    //搜索学生
    queryStudentInfo({commit},info) { //查询学生
        return new Promise((resolve,reject) => {
            searchStu(info).then((data) => {
                //console.log(data)
                if(data.code === 1){
                    commit('SET_SIMPLE_STUDENT_INFO',data.data.data)
                    commit('SET_COUNT',data.data.count)
                    resolve(data);
                }else {
                    reject(data.msg);
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },
}


export default {
    namespaced: true,
    state,
    mutations,
    actions
}
